package com.logic.saas.util.interceptor;

import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.StrutsStatics;

import com.logic.saas.enums.SessionKey;
import com.logic.saas.pojo.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.LocaleProvider;
import com.opensymphony.xwork2.TextProviderSupport;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

public class ChangeLocaleInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = -5553085378251912515L;
	private Logger logger = Logger.getLogger(ChangeLocaleInterceptor.class);

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
	    ActionContext actionContex = invocation.getInvocationContext();
	    HttpServletRequest request = (HttpServletRequest) actionContex.get(StrutsStatics.HTTP_REQUEST);
	    String localeStr = request.getParameter("request_locale");
	    if (StringUtils.isNotBlank(localeStr)) {
	        final Locale locale;
	        if (localeStr.endsWith("zh_CN")) {
	            locale = Locale.SIMPLIFIED_CHINESE;
            }else{
                locale = Locale.US;
            }
	        
            User user = (User)ActionContext.getContext().getSession().get(SessionKey.USER);
            TextProviderSupport text = new  TextProviderSupport();
            text.setClazz(ChangeLocaleInterceptor.class);
            text.setLocaleProvider( new LocaleProvider() {
                public Locale getLocale() {
                    return locale;
                }
            });
          
            ActionContext.getContext().getSession().put(SessionKey.LANG_INT, Locales.getLangint(localeStr));
        }
	    
	    return invocation.invoke();
	}
	
    
}
